package com.hrbxlh.virtualization.controller.manage;

import com.github.pagehelper.PageInfo;
import com.hrbxlh.virtualization.result.Result;
import com.hrbxlh.virtualization.service.ImageService;
import com.hrbxlh.virtualization.vo.ImageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/manage/image")
public class ManageImageController {

    @Autowired
    private ImageService imageService;

    /**
     * 查询镜像列表
     *
     * @param isInitial 是否为预置镜像
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<List<ImageVO>> listImages(@RequestParam(required = false) Boolean isInitial) {
        List<ImageVO> imageVOList = imageService.listImages(isInitial);
        return Result.success(imageVOList);
    }

    /**
     * 分页查询镜像列表
     *
     * @param pageNum  页数
     * @param pageSize 每页大小
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public Result<PageInfo<ImageVO>> listImagesByPage(@RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize) {

        PageInfo<ImageVO> page = imageService.listImagesByPage(pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 删除镜像
     *
     * @param id       镜像Id
     * @param isForced 是否强制删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Result deleteImage(Long id, @RequestParam(required = false, defaultValue = "false") Boolean isForced) {
        imageService.deleteImage(id, isForced);
        return Result.success();
    }

}
